package cn.iocoder.yudao.module.asasc.controller.admin.campaign;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.AsaAppConfigPageReqVO;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.AsaAppConfigRespVO;
import cn.iocoder.yudao.module.asasc.controller.admin.campaign.vo.AsaAppConfigSaveReqVO;
import cn.iocoder.yudao.module.asasc.dal.dataobject.campaign.AsaAppConfigDO;
import cn.iocoder.yudao.module.asasc.service.campaign.AsaAppConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ASA应用配置")
@RestController
@RequestMapping("/asasc/asa-app-config")
@Validated
public class AsaAppConfigController {

    @Resource
    private AsaAppConfigService asaAppConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建ASA应用配置")
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:create')")
    public CommonResult<Integer> createAsaAppConfig(@Valid @RequestBody AsaAppConfigSaveReqVO createReqVO) {
        return success(asaAppConfigService.createAsaAppConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ASA应用配置")
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:update')")
    public CommonResult<Boolean> updateAsaAppConfig(@Valid @RequestBody AsaAppConfigSaveReqVO updateReqVO) {
        asaAppConfigService.updateAsaAppConfig(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新定时任务的状态")
    @Parameters({
            @Parameter(name = "id", description = "编号", required = true, example = "1024"),
            @Parameter(name = "status", description = "状态", required = true, example = "1"),
    })
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:update')")
    public CommonResult<Boolean> updateJobStatus(@RequestParam(value = "id") Integer id,
                                                 @RequestParam("jobEnable") Integer jobEnable)
            throws SchedulerException {
        asaAppConfigService.updateJobStatus(id, jobEnable);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ASA应用配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:delete')")
    public CommonResult<Boolean> deleteAsaAppConfig(@RequestParam("id") Integer id) {
        asaAppConfigService.deleteAsaAppConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ASA应用配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:query')")
    public CommonResult<AsaAppConfigRespVO> getAsaAppConfig(@RequestParam("id") Integer id) {
        AsaAppConfigDO asaAppConfig = asaAppConfigService.getAsaAppConfig(id);
        return success(BeanUtils.toBean(asaAppConfig, AsaAppConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ASA应用配置分页")
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:query')")
    public CommonResult<PageResult<AsaAppConfigRespVO>> getAsaAppConfigPage(@Valid AsaAppConfigPageReqVO pageReqVO) {
        PageResult<AsaAppConfigDO> pageResult = asaAppConfigService.getAsaAppConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AsaAppConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ASA应用配置 Excel")
    @PreAuthorize("@ss.hasPermission('asasc:asa-app-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAsaAppConfigExcel(@Valid AsaAppConfigPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AsaAppConfigDO> list = asaAppConfigService.getAsaAppConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ASA应用配置.xls", "数据", AsaAppConfigRespVO.class,
                BeanUtils.toBean(list, AsaAppConfigRespVO.class));
    }

}